﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using Public;

namespace DAL
{
    public class Kho_DAL
    {
        Connection conn = new Connection();

        #region "Lấy DS Kho"
        public DataTable LayDSKho()
        {
            return conn.LoadData("select * from KHO");
        }
        #endregion

        #region "Thêm Kho"
        public int ThemSuaKho(Kho_PB k_pb)
        {
            int nparameter = 3;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@makho"; value[0] = Kho_PB.MaKho;
            name[1] = "@tenkho"; value[1] = Kho_PB.TenKho;
            name[2] = "@diachi"; value[2] = Kho_PB.DiaChi;
            string sql = @"if exists (select * from KHO where MAKHO = @makho)
                                 update KHO set TENKHO=@tenkho,DIACHI=@diachi where MAKHO=@makho
	                     else
                                insert into KHO values (@makho,@tenkho,@diachi)
                         ";
            return conn.Update(sql, name, value, nparameter);
        }
        #endregion

        //#region "Sửa kho"
        //public int SuaKho(Kho_PB k_pb)
        //{
        //    int nparameter = 3;
        //    string[] name = new string[nparameter];
        //    object[] value = new object[nparameter];
        //    name[0] = "@makho"; value[0] = Kho_PB.MaKho;
        //    name[1] = "@tenkho"; value[1] = Kho_PB.TenKho;
        //    name[2] = "@diachi"; value[2] = Kho_PB.DiaChi;
        //    string sql = "update KHO set TENKHO=@tenkho,DIACHI=@diachi where MAKHO=@makho";
        //    return conn.Update(sql, name, value, nparameter);
        //}
        //#endregion

        #region "Xóa kho"
        public int XoaKho(Kho_PB k_pb)
        {
            int nparameter = 3;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@makho"; value[0] = Kho_PB.MaKho;
            name[1] = "@tenkho"; value[1] = Kho_PB.TenKho;
            name[2] = "@diachi"; value[2] = Kho_PB.DiaChi;
            string sql = "delete from KHO where MAKHO=@makho";
            return conn.Update(sql, name, value, nparameter);
        }
        public int XoaKho(string ma)
        {
            int nparameter = 1;
            string[] name = new string[nparameter];
            object[] value = new object[nparameter];
            name[0] = "@makho"; value[0] = ma;
            string sql = "delete from KHO where MAKHO=@makho";
            return conn.Update(sql, name, value, nparameter);
        }
        #endregion
        public bool Exists(string ma)
        {
            string sql = string.Format("select * from KHO where MAKHO = '{0}'", ma);
            DataTable dt = conn.LoadData(sql);
            if (dt == null) return false;

            return dt.Rows.Count > 0;
        }
    }
}
